//
//  MusicTableViewController.m
//  DownLoadByNSURLSession
//
//  Created by @Zs on 16/4/18.
//  Copyright © 2016年 @Zs. All rights reserved.
//

#import "MusicTableViewController.h"
#import "MusicCell.h"

@interface MusicTableViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic , strong) NSArray *cellDataArray;

@end

@implementation MusicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.userInteractionEnabled = YES;
    [self initProperty];
    [self addGesture];

}

- (void)initProperty
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *plistPath = [cachePath stringByAppendingPathComponent:@"music.plist"];
    NSArray *plistArray = [NSArray arrayWithContentsOfFile:plistPath];
    
    
    if (plistArray) {
        _cellDataArray = plistArray;
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _cellDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    MusicCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[MusicCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    [cell cellInoWithDictionary:_cellDataArray[indexPath.row]];
    
    return cell;
}


/**
 *  添加手势
 */
-(void)addGesture{
    
    NSLog(@"添加手势");
    
    UISwipeGestureRecognizer *swipeGestrueToDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cancelMusic:)];
    swipeGestrueToDown.delegate = self;
    [self.tableView addGestureRecognizer:swipeGestrueToDown];
    [swipeGestrueToDown setDirection:UISwipeGestureRecognizerDirectionDown];
    
}

-(void)cancelMusic:(UISwipeGestureRecognizer *)gesture{

    if (gesture.direction == UISwipeGestureRecognizerDirectionDown) {
        [self dismissViewControllerAnimated:YES completion:^{
            NSLog(@"移除音乐播放界面");
        }];
    }
}


//#pragma mark 手势代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([otherGestureRecognizer.view isKindOfClass:[UITableView class]]) {
        return YES;
    }
    return NO;
}


@end
